<template>
  <div class="app-container">
    <el-card class="box-card" :body-style="{ padding: '20px' }">
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        :header-cell-style="{
          background: '#f5f7fa',
          color: '#606266',
          fontWeight: 600
        }"
        :row-class-name="tableRowClassName"
        border
      >
        <el-table-column prop="name" label="姓名" width="180" sortable>
          <template #default="scope">
            <span class="table-cell-text">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="gender" label="性别" width="100">
          <template #default="scope">
            <el-tag
              :type="scope.row.gender === '男' ? 'primary' : 'success'"
              effect="light"
              class="gender-tag"
            >{{ scope.row.gender }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="120" sortable>
          <template #default="scope">
            <span class="table-cell-text">{{ scope.row.age }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="地址">
          <template #default="scope">
            <span class="table-cell-text">{{ scope.row.address }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="注册日期" width="180" sortable>
          <template #default="scope">
            <span class="table-cell-text">{{ scope.row.date }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button
              size="small"
              type="primary"
              plain
              class="table-button"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              size="small"
              type="danger"
              plain
              class="table-button"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          class="custom-pagination"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页参数
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 模拟获取数据
const getTableData = () => {
  loading.value = true
  // 模拟异步请求
  setTimeout(() => {
    const mockData = Array.from({ length: 100 }, (_, index) => ({
      id: index + 1,
      name: `用户${index + 1}`,
      gender: Math.random() > 0.5 ? '男' : '女',
      age: Math.floor(Math.random() * 50) + 18,
      address: `中国省市区街道第${index + 1}号`,
      date: new Date(Date.now() - Math.floor(Math.random() * 10000000000)).toLocaleDateString()
    }))
    
    total.value = mockData.length
    const start = (currentPage.value - 1) * pageSize.value
    const end = start + pageSize.value
    tableData.value = mockData.slice(start, end)
    loading.value = false
  }, 500)
}

// 处理页码改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  getTableData()
}

// 处理每页条数改变
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

// 处理编辑
const handleEdit = (row) => {
  ElMessage.success(`编辑用户：${row.name}`)
}

// 处理删除
const handleDelete = (row) => {
  ElMessage.warning(`删除用户：${row.name}`)
}

// 初始化加载数据
onMounted(() => {
  getTableData()
})
</script>

<style scoped>
.app-container {
  padding: 20px;
  background-color: #f0f2f5;
}

.box-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.table-cell-text {
  color: #606266;
  font-size: 14px;
}

.gender-tag {
  padding: 4px 12px;
  border-radius: 4px;
  font-weight: 500;
}

.table-button {
  padding: 6px 16px;
  margin: 0 4px;
  font-weight: 500;
  transition: all 0.3s;
}

.table-button:hover {
  transform: translateY(-1px);
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.custom-pagination {
  padding: 4px 0;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table__row) {
  transition: background-color 0.3s;
}

:deep(.el-table__row:hover) {
  background-color: #f5f7fa !important;
}

:deep(.el-table__row:nth-child(even)) {
  background-color: #fafafa;
}
</style>